package presentation.windows;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import java.awt.Dimension;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;

import presentation.UIPost;

import util.Log;
import util.NotFoundException;
import util.Subsystem;
import domain.user.UnpermittedActionException;




/**
 *  This is the screen for editing a post in the forum
 */
public class EditPostFrame extends javax.swing.JFrame
{

	private static final long serialVersionUID = 3044515939367585787L ;

	private JTextArea postText;
	private JButton okPost;
	private JLabel addLable;
	private JScrollPane jScrollPane1;
	private JButton cancelPost;

	ViewThreadPanel threadPanel;
	UIPost post;

	/**
	 * Constructor
	 * @param viewThreadPanel -  the thread in which the post is edited
	 * @param post - the post that's being edited
	 */
	public EditPostFrame(final ViewThreadPanel viewThreadPanel, final UIPost post)
	{
		this.setPreferredSize(new Dimension(735, 566));
		this.threadPanel = viewThreadPanel;
		this.post = post;
		this.initGUI();
	}

	/**
	 * Initialize the editMessage screen
	 */
	public void initGUI()
	{
		AnchorLayout thisLayout = new AnchorLayout();
		getContentPane().setLayout(thisLayout);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Editing message");

			this.postText = new JTextArea();
			this.postText.setText(this.post.getMessage());
			this.jScrollPane1 = new JScrollPane();
			getContentPane().add(this.jScrollPane1, new AnchorConstraint(126, 918, 846, 25, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.jScrollPane1.setPreferredSize(new java.awt.Dimension(649, 383));
			this.jScrollPane1.setViewportView(this.postText);

			this.okPost = new JButton();
			this.okPost.setText("OK");
			this.okPost.setPreferredSize(new java.awt.Dimension(73, 34));
			getContentPane().add(this.okPost, new AnchorConstraint(869, 218, 933, 117, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

			this.okPost.addActionListener(new java.awt.event.ActionListener() {
		         @SuppressWarnings("synthetic-access")
				public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
		        	 EditPostFrame.this.okButtonHandler();
		         }
			 });

			this.cancelPost = new JButton();
			this.cancelPost.setText("cancel");
			this.cancelPost.setPreferredSize(new java.awt.Dimension(76, 34));
			getContentPane().add(this.cancelPost, new AnchorConstraint(869, 416, 933, 311, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

			this.cancelPost.addActionListener(new java.awt.event.ActionListener() {
		         @SuppressWarnings("synthetic-access")
				public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
		        	 EditPostFrame.this.cancelButtonHandler();
		         }
			 });

			this.addLable = new JLabel();
			this.addLable.setText("edit your message here:");
			this.addLable.setPreferredSize(new java.awt.Dimension(207, 14));
			getContentPane().add(this.addLable, new AnchorConstraint(78, 311, 104, 26, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			

	}

	private void okButtonHandler()
	{
		
		new SwingWorker<Void,Void>(){
			
			@SuppressWarnings("synthetic-access")
			@Override
			protected Void doInBackground(){
				

				String messageText = EditPostFrame.this.postText.getText();
				if(messageText==null || messageText.equals(""))
				{
					//no message insertion:
					JOptionPane.showMessageDialog(EditPostFrame.this,
						"please enter a message", "error occured", JOptionPane.ERROR_MESSAGE);
				}
				else{
					try
					{
						EditPostFrame.this.threadPanel.editPost(messageText,EditPostFrame.this.post);
						EditPostFrame.this.dispose(); //close the window
					}
					catch (RemoteException e)
					{
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("communication problem when trying to edit message.") ;
						//show error message:
						JOptionPane.showMessageDialog(EditPostFrame.this,
							"problem with editing the message", "error occured", JOptionPane.ERROR_MESSAGE);
					}
					catch (UnpermittedActionException e)
					{
						long userId = EditPostFrame.this.threadPanel.getSharedData().getUser().getId();
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+userId+" is unpermitted to edit this message.") ;
						//show error message:
						JOptionPane.showMessageDialog(EditPostFrame.this,
							"you are unpermitted to edit this message", "error occured", JOptionPane.ERROR_MESSAGE);
						EditPostFrame.this.cancelButtonHandler(); //close the window - this is not recoverable error!
					}
					catch (NotFoundException e)
					{
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("the wanted thread to add message to could not be found.") ;
						//show error message:
						JOptionPane.showMessageDialog(EditPostFrame.this,
							"problem with editing the message", "error occured", JOptionPane.ERROR_MESSAGE);
						EditPostFrame.this.cancelButtonHandler(); //close the window - this is not recoverable error!
					}
				}
				return null;
			}
			@Override
			protected void done(){/*nothing to do here */}
		}.execute();
	}

	private void cancelButtonHandler()
	{
		this.threadPanel.makeVisible(); //to refresh the window we are going back to
		this.dispose(); //close the window
	}
	
	/**
	 * @see java.awt.Window#dispose()
	 */
	@Override
	public void dispose()
	{
		super.dispose() ;
		this.threadPanel.removeOpenSubFrame(this);
	}


}

